<?php
class User extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
	}
	
	function userList() {
		if (($this->session->userdata ( 'authority' ) === '0')) {
			$this->load->model ( 'Muser' );
			$this->load->library ( 'pagination' );
			
			$data ['num'] = $this->Muser->getUserNum ();
			$data ['page'] = $this->uri->segment ( 4, 0 );
			
			$config ['base_url'] = base_url () . '/admin/user/userList/';
			$config ['total_rows'] = $data ['num'];
			$config ['per_page'] = PAGE_SHOW_NUM;
			$config ['uri_segment'] = 4;
			$config ['num_links'] = 2;
			$this->pagination->initialize ( $config );
			
			$data ['list'] = $this->Muser->getUserList ( $data ['page'], PAGE_SHOW_NUM );
			
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/user_list' );
			$this->load->view ( 'admin/footer' );
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有管理用户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function deleUser() {
		if ($this->session->userdata ( 'authority' ) === '0') {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Muser' );
			
			if ($this->Muser->deleteUser ( $data ['id'] )) {
				$data ['msg'] = '用户删除成功！';
			} else
				$data ['msg'] = '用户删除失败！';
			
			$data ['url'] = base_url () . 'admin/user/userList';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有删除用户的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function download() {
		if ($this->session->userdata ( 'authority' ) === '0') {
			$this->form_validation->set_rules ( 'begin', '开始编号', 'trim|numeric|callback_checkBeginCB' );
			$this->form_validation->set_rules ( "end", '结束编号', 'trim|numeric|callback_checkEndCB' );
			
			$this->load->model ( 'Muser' );
			
			if ($this->form_validation->run () == FALSE) {
				$data ['form'] = array ('id' => 'downloadForm' );
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/user_download' );
				$this->load->view ( 'admin/footer' );
			} else {
				$begin = $this->input->post ( 'begin' );
				$end = $this->input->post ( 'end' );
				
				$filePath = './uploads/business/';
				require_once 'application/libraries/PHPExcel.php';
				require_once 'application/libraries/PHPExcel/Writer/Excel2007.php';
				require_once 'application/libraries/PHPExcel/Writer/Excel5.php';
				include_once 'application/libraries/PHPExcel/IOFactory.php';
				
				$objExcel = new PHPExcel ();
				$objExcel->getProperties ()->setCreator ( "newstar" );
				$objExcel->setActiveSheetIndex ( 0 );
				
				$i = 1;
				if (! $begin)
					$begin = 1;
				$num = $this->Muser->getUserMaxNum ();
				if (! $end)
					$end = $num;
				$objExcel->getActiveSheet ()->setCellValue ( 'a1', "用户信息记录表" );
				$objExcel->getActiveSheet ()->setCellValue ( 'a2', "编号" );
				$objExcel->getActiveSheet ()->setCellValue ( 'b2', "邮箱" );
				$objExcel->getActiveSheet ()->setCellValue ( 'c2', "姓名" );
				$objExcel->getActiveSheet ()->setCellValue ( 'd2', "手机" );
				$objExcel->getActiveSheet ()->setCellValue ( 'e2', "激活码" );
				$objExcel->getActiveSheet ()->setCellValue ( 'f2', "重置码" );
				$objExcel->getActiveSheet ()->setCellValue ( 'g2', "创建时间" );
				$objExcel->getActiveSheet ()->setCellValue ( 'h2', "登录时间" );
				$objExcel->getActiveSheet ()->setCellValue ( 'i2', "登录IP" );
				$query = NULL;
				if ($end)
					$query = $this->Muser->getUserListforDownload ( $begin, $end );
				if ($query)
					foreach ( $query as $v ) {
						$u1 = $i + 2;
						/*----------写入内容-------------*/
						$objExcel->getActiveSheet ()->setCellValue ( 'a' . $u1, $v ["id"] );
						$objExcel->getActiveSheet ()->setCellValue ( 'b' . $u1, $v ["email"] );
						$objExcel->getActiveSheet ()->setCellValue ( 'c' . $u1, $v ["name"] );
						$objExcel->getActiveSheet ()->setCellValue ( 'd' . $u1, $v ["mobile"] );
						$objExcel->getActiveSheet ()->setCellValue ( 'e' . $u1, $v ["activation"] );
						$objExcel->getActiveSheet ()->setCellValue ( 'f' . $u1, $v ["reset"] );
						$objExcel->getActiveSheet ()->setCellValue ( 'g' . $u1, $v ["creatTime"] );
						$objExcel->getActiveSheet ()->setCellValue ( 'h' . $u1, $v ["loginTime"] );
						$objExcel->getActiveSheet ()->setCellValue ( 'i' . $u1, $v ["loginIp"] );
						$i ++;
					}
				
				$objExcel->getActiveSheet ()->getColumnDimension ( 'A' )->setWidth ( 6 );
				$objExcel->getActiveSheet ()->getColumnDimension ( 'B' )->setWidth ( 18 );
				$objExcel->getActiveSheet ()->getColumnDimension ( 'C' )->setWidth ( 10 );
				$objExcel->getActiveSheet ()->getColumnDimension ( 'D' )->setWidth ( 10 );
				$objExcel->getActiveSheet ()->getColumnDimension ( 'E' )->setWidth ( 10 );
				$objExcel->getActiveSheet ()->getColumnDimension ( 'F' )->setWidth ( 10 );
				$objExcel->getActiveSheet ()->getColumnDimension ( 'G' )->setWidth ( 18 );
				$objExcel->getActiveSheet ()->getColumnDimension ( 'H' )->setWidth ( 18 );
				$objExcel->getActiveSheet ()->getColumnDimension ( 'I' )->setWidth ( 10 );
				
				$timestamp = time ();
				$objExcel->getActiveSheet ()->mergeCells ( 'A1:I1' );
				$objExcel->getActiveSheet ()->getStyle ( 'A1:I1' )->getAlignment ()->setHorizontal ( PHPExcel_Style_Alignment::HORIZONTAL_CENTER );
				
				header ( 'Content-Type: application/vnd.ms-excel' );
				header ( 'Content-Disposition: attachment;filename="user.xls"' );
				header ( 'Cache-Control: max-age=0' );
				
				$objWriter = PHPExcel_IOFactory::createWriter ( $objExcel, 'Excel5' );
				$objWriter->save ( 'php://output' );
			}
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有群发邮件的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function mailUsers() {
		if ($this->session->userdata ( 'authority' ) === '0') {
			$this->form_validation->set_rules ( 'title', '邮件标题', 'trim|required|max_length[100]' );
			
			if ($this->form_validation->run () == FALSE) {
				$data ['form'] = array ('id' => 'questionAddForm' );
				$data ['content'] = $this->input->post ( 'details' );
				
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/user_mailAll' );
				$this->load->view ( 'admin/footer' );
			} else {
				$this->load->model ( 'Muser' );
				$query = $this->Muser->selectAllUserforMail ();
				
				error_reporting ( E_STRICT );
				date_default_timezone_set ( "Asia/Shanghai" );
				require_once ('application/libraries/class.phpmailer.php');
				include ("application/libraries/class.smtp.php");
				
				$mail = new PHPMailer ();
				$mail->CharSet = "UTF-8";
				$mail->IsSMTP ();
				$mail->SMTPDebug = 1;
				$mail->SMTPAuth = true;
				$mail->SMTPSecure = "";
				$mail->Host = "1.1.2.5";
				$mail->Port = 25;
				$mail->Username = "nsdkj@nsdkj.com.cn";
				$mail->Password = "nsdkj101010";
				$mail->SetFrom ( 'nsdkj@nsdkj.com.cn', '纽斯达' );
				$mail->Subject = $this->input->post ( 'title' );
				$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
				
				$data ['msg'] = '发送错误！用户编号：';
				$flag = TRUE;
				foreach ( $query as $v ) {
					$mail->MsgHTML ( $this->input->post ( 'details' ) );
					$mail->AddAddress ( $v ['email'], $v ['name'] );
					if (! $mail->Send ()) {
						$flag = FALSE;
						$data ['msg'] .= ' ' . $v [id];
					}
					$mail->ClearAddresses ();
				}
				if ($flag) {
					$data ['msg'] = '邮件发送成功！';
				} else
					$data ['msg'] .= '未成功发送！';
				
				$data ['url'] = base_url () . 'admin/user/mailUsers';
				$this->load->view ( 'admin/admin_message', $data );
			}
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有群发邮件的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	//功能函数
	function checkBeginCB($str) {
		if ($str) {
			$this->load->model ( 'Muser' );
			$num = $this->Muser->getUserMaxNum ();
			if ($str > 0 && $str <= $this->input->post ( 'end' ) && $str <= $num)
				return TRUE;
			else {
				$this->form_validation->set_message ( 'checkBeginCB', '%s不正确' );
				return FALSE;
			}
		} else
			return TRUE;
	}
	
	function checkEndCB($str) {
		if ($str) {
			$this->load->model ( 'Muser' );
			$num = $this->Muser->getUserMaxNum ();
			if ($str > 0 && $str >= $this->input->post ( 'begin' ) && $str <= $num)
				return TRUE;
			else {
				$this->form_validation->set_message ( 'checkEndCB', '%s不正确' );
				return FALSE;
			}
		} else
			return TRUE;
	}
}